import yh from "../../../tools/yh";

class KeyboardController {
	constructor() {
		/** 键盘列表 */
		this._keyboardList = {};
		/** 当前键盘 */
		this._current = null;
		/** 计数 */
		this._index = 0;

		this._CloseResolve = null;
	}

	/** 销毁控制器 */
	_destroy(index) {
		this._hideKeyboard();
		this._clear();
		this._removeKeyboardEvent(index);
	}
	/** 注册一个键盘 ,目前键盘只有在完成和收起时可以获取加密后的输入值
	 * {
	 *   键盘输入完成/收起
	 *   onKeyComplete:()=>{}
	 *   键盘输入
	 *   onKeyInput:()=>{}
	 *   键盘删除键
	 *   onKeyRemove:()=>{}
	 *   激活键盘激活键盘时会清空所有数据
	 *   onAction:()=>{}
	 *   或者失去焦点
	 *   onNoAction:()=>{}
	 * }
	 */
	registerKeyboard(o) {
		let _index = this._index++;
		let _k = {
			...o,
			isOpen: false,
			HTkeyboardSure: res => {
				console.log("确定：" + JSON.stringify(res));
				let text = res.text && res.text.indexOf("PIN") > -1 ? "" : res.text;
				// if(res.text&&res.text.indexOf('PIN')===-1){
				o.onKeyComplete(text);
				this._clear();
				// }
			},
			HTkeyboardInput: res => {
				console.log("输入：" + JSON.stringify(res));
				o.onKeyInput();
			},
			HTkeyboardDelete: res => {
				console.log("删除：" + JSON.stringify(res));
				o.onKeyRemove();
			}
		};
		this._keyboardList[_index] = _k;
		return {
			_index,
			/** 打开键盘 */
			open: () => this._openKeyboard(_index),
			close: () => this._hideKeyboard(),
			destroy: () => this._destroy(_index)
		};
	}

	/** 打开键盘 */
	_openKeyboard(index) {
		let _k = this._keyboardList[index];
		let _old = this._keyboardList[this._current];
		if (_old) {
			_old.isOpen = false;
		}
		if (!_k.isOpen) {
			if (this._current !== index) {
				this._keyboardList[this._current] &&
					this._keyboardList[this._current].onNoAction();
			}

			if (this._current !== null) {
				let oldindex = this._current;
				// if(this._current !== index){
				//     this._hideKeyboard();
				// }
				this._clear();
				this._removeKeyboardEvent(oldindex);
			}
			this._showKeyboard();
			this._initKeyboardEvent(index);
			this._keyboardList[index] && this._keyboardList[index].onAction();
			_k.isOpen = true;
			this._current = index;
		}
	}

	/**显示 */
	_showKeyboard() {
		yh.showHTkeyboard({
			actionType: "show"
		});
	}

	/** 关闭键盘 */
	_hideKeyboard() {
		yh.showHTkeyboard({
			actionType: "close"
		});
	}

	/**初始化键盘事件 */
	_initKeyboardEvent(index) {
		if (!window.YHAuthorize || !window.YHAuthorize.View) return;
		let _k = this._keyboardList[index];
		// 确定按钮/键盘收起（返回加密后的数据{text:加密数据}）
		window.YHAuthorize.View.addEventListener(
			"HTkeyboardSure",
			_k.HTkeyboardSure
		);
		// 输入改变
		window.YHAuthorize.View.addEventListener(
			"HTkeyboardInput",
			_k.HTkeyboardInput
		);
		// 删除按钮
		window.YHAuthorize.View.addEventListener(
			"HTkeyboardDelete",
			_k.HTkeyboardDelete
		);
	}
	/**卸载键盘 */
	_removeKeyboardEvent(index) {
		let _k = this._keyboardList[index];
		window.YHAuthorize.View.removeEventListener(
			"HTkeyboardSure",
			_k.HTkeyboardSure
		);
		window.YHAuthorize.View.removeEventListener(
			"HTkeyboardInput",
			_k.HTkeyboardInput
		);
		window.YHAuthorize.View.removeEventListener(
			"HTkeyboardDelete",
			_k.HTkeyboardDelete
		);
	}
	/** 清除键盘内容 */
	_clear() {
		yh.showHTkeyboard({
			actionType: "clear"
		});
	}
}

export default new KeyboardController();
